<?php

namespace app\api\controller\hezi;

use app\admin\model\JingpinUser;
use app\admin\model\JingpinTask;
use app\admin\model\JingpinUserTask;
use app\common\controller\Api;
use think\cache\driver\Redis;
use think\Log;
use think\Request;

class Jingpintasks extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 任务列表
    public function task_list(Request $request)
    {
        $openid = $request->param('openid');
        $ntime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));

        $longitem = JingpinTask::all(['type' => '1']);
        foreach ($longitem as $item) {
            $item->progress = 0;
            $item->is_finish = 0;
            if ($task = JingpinUserTask::get(['openid' => $openid, 'taskid' => $item->id])) {
                $item->progress = $task->progress;
                $item->is_finish = $task->is_finish;
            }
        }
        $daliy = JingpinTask::all(['type' => '2']);
        foreach ($daliy as $item) {
            if ($task = JingpinUserTask::get(['openid' => $openid, 'taskid' => $item->id])) {
                if ($task->updatetime < $ntime) {
                    $task->progress = 0;
                    $task->is_finish = 0;
                }
                $item->progress = $task->progress;
                $item->is_finish = $task->is_finish;
                $task->save();
            } else {
                $item->progress = 0;
                $item->is_finish = 0;
            }
        }
        $list['longitem'] = $longitem;
        $list['daliy'] = $daliy;
        $this->result('', $list);
    }

    //签到天数
    public function singn_list()
    {
        $list = array(
            array('day' => '1天', 'gold' => '12',),
            array('day' => '2天', 'gold' => '18',),
            array('day' => '3天', 'gold' => '24',),
            array('day' => '4天', 'gold' => '36',),
            array('day' => '5天', 'gold' => '48',),
            array('day' => '6天', 'gold' => '72',),
            array('day' => '7天', 'gold' => '128',),
        );
        $this->result('', $list);
    }

    //获取签到信息
    public function get_signininfo(Request $request)
    {
        $openid = $request->param('openid');
        $user = JingpinUser::get(['openid' => $openid]);
        $ntime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $msg = 0;
        if ($user->prev_signin > $ntime){
            $msg = 1;
        }
        $this->result($msg, $user->signin_num);
    }

    //签到成功
    public function signin_success(Request $request)
    {
        $openid = $request->param('openid');
        $gold = $request->param('gold');
        $list = JingpinUser::get(['openid' => $openid]);
        $ntime = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        if ($list->prev_signin > $ntime) {
            $this->result('今天已经签到过了', '');
        } else {
            $list->signin_num = $list->signin_num + 1;
            $list->gold = $list->gold + $gold;
            $list->prev_signin = time();
            $data = $list->save();
            if ($data) {
                $appidlist = array(
                    'wxbdd0d9a5187bc731',
                    'wx3093c5ec75efcd7b',
                    'wxfc2e85ad6e4c1691',
                    'wx1ce4c0d710fcec54',
                    'wx07cac980fde4202a',
                    'wx38e4080448c98296',
                    'wx77a9968ac2c2d9e7',
                    'wxa626926803a7a03b',
                    'wx72aba8c49ad40eaa',
                    'wx8325192a2cf3611c',
                );
                $num = mt_rand(0, 9);
                $this->result('', $appidlist[$num]);
            }
        }
    }

    // 领取任务奖励
    public function get_task_award(Request $request)
    {
        $openid = $request->param('openid');
        $taskid = $request->param('taskid');

        $user = JingpinUser::get(['openid' => $openid]);
        $task = JingpinTask::get($taskid);
        $award = $task->task_award;

        $user_task = JingpinUserTask::get(['openid' => $openid, 'taskid' => $taskid]);
        if ($user_task) {
            if ($user_task->is_finish == 1) {
                $user_task->is_finish = 2;
                $user->gold += $award;
                $user_task->save();
                $user->save();
                $this->success('ok');
            }
        }
        $this->error('error');
    }

    // 完成进入游戏任务
    public function game_task_complete(Request $request)
    {
        $openid = $request->param('openid');
        $this->update_user_task($openid, 'game');
    }

    // 完成分享邀请任务
    public function share_task_complete(Request $request)
    {
        $from_openid = $request->param('from_openid'); // 来源用户
        $to_openid = $request->param('to_openid'); // 邀请用户

        // 获取被邀请用户id
        $to_id = JingpinUser::where('openid', $to_openid)->value('id');

        $redis = new Redis();
        if($redis->has($from_openid . '_share')) {
            $share_ids_str = $redis->get($from_openid . '_share');
            $share_ids = explode(',', $share_ids_str);
            // 判断是否大于10
            if (count($share_ids) >= 10) {
                $this->error('error');
            }
            // 判断重复用户
            foreach ($share_ids as $id) {
                if ($id == $to_id) {
                    $this->error('error');
                }
            }
            $redis->set($from_openid . '_share', $share_ids_str.','.$to_id);
        } else {
            $redis->set($from_openid . '_share', $to_id);
        }
        $this->update_user_task($from_openid, 'share');
    }

    public function qun_task_complete(Request $request)
    {

    }

    // 更新用户任务
    public function update_user_task($openid, $task_type)
    {
        $tasks = JingpinTask::all(['task_type' => $task_type]);
        foreach ($tasks as $task) {
            $user_task = JingpinUserTask::get(['openid' => $openid, 'taskid' => $task->id]);
            if ($user_task) {
                if ($user_task->is_finish == 0) {
                    $user_task->progress += 1;
                    $user_task->is_finish = $user_task->progress >= $task->task_condition ? 1 : 0;
                    $user_task->save();
                }
            } else {
                $ut = new JingpinUserTask();
                $ut->openid = $openid;
                $ut->type = $task->type;
                $ut->taskid = $task->id;
                $ut->progress = 1;
                $ut->is_finish = $ut->progress >= $task->task_condition ? 1 : 0;
                $ut->save();
            }
        }
    }
}
